package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    //注入MenuService
    @Reference
    private MenuService menuService;

    /*
        查询所有一级菜单
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findParentMenus")
    public Result findParentMenus() {
        try {
            //调用服务层
            List<Menu> menuList = menuService.findParentMenus();
            return new Result(true, MessageConstant.QUERY_PMENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PMENU_FAIL);
        }
    }

    /*
        根据父级id查询所有子级菜单
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findChildrenMenusById")
    public Result findChildrenMenusById(Integer parentId) {
        try {
            //调用服务层
            List<Menu> menuList = menuService.findChildrenMenusById(parentId);
            return new Result(true, MessageConstant.QUERY_PMENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PMENU_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('MENU_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody Menu menu){
        System.out.println(menu);
        try{
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }


    /*
       分页查询
    */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用业务层
        PageResult pageResult = menuService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    /*
      根据id查询
   */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            //调用业务层
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /*
        修改
     */
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS, menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /*
      删除
   */
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //调用业务层
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        }catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /*
    查找所有父级菜单
     */
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List list = menuService.findAll();
            return new Result(true,MessageConstant.MENU_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.MENU_FAIL);
        }
    }
}
